﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using WPFExcelReport.Helpers;
using System.Data.OracleClient;
using System.IO;
using INTEX.DataBase.Tools;
using INTEX.DataBase;
using System.Linq;
namespace WPFExcelReport
{
    public partial class YCAEK02LISTReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new INTEX.DataBase.SETINTEXDB();
        #endregion
        public YCAEK02LISTReport()
            : base()
        {


        }
        public YCAEK02LISTReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
        }
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                //exAgent._app.Visible = true;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                strCompany = "YCA";
                strExcelFile = @"YCA\YCAEK02LIST.xls";
                Standard = "EK02LIST";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\YCA\YCAEK02LIST.xls");
                destfolder = SetFolder(Root, "采购评估");
                INTEXDBC.OracleDB = OracleDB;
                INTEXDBC.proxy = proxy; 

                string newfilename =year + "_" + month + "_" + day;
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile(newfilename, fileType);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\采购评估" + fileabsname + ".xls"; ;
                destfile = filename;
                if (File.Exists(fileabsname + ".xls"))
                {
                    File.Delete(fileabsname + ".xls");
                }
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        public void FillInExcelData(int xStart = 1, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = dsSource.Tables["EKPOS"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("LSLIEFNA", typeof(System.String));
                dtOutput.Columns.Add("FSKOLL", typeof(System.String));                
                dtOutput.Columns.Add("FSARTPRGR", typeof(System.String));                
                dtOutput.Columns.Add("EKPMEEH", typeof(System.String));
                dtOutput.Columns.Add("EKPME1", typeof(System.String));
                dtOutput.Columns.Add("EKPWE1", typeof(System.String));              
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                exAgent.DataTableToExcel(dtOutput, xStart, yStart);
                int count = dtOutput.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(2, 5, count + 1, 6);
                exAgent.SetCellValue(1, 1, "供应商评估");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private string CreateFile(string ordernumer)
        {
            string filename = "";
            string sheetName = "";
            string newfilename = "";
            try
            {
                string pwd = "";
                sheetName = ordernumer;
                newfilename = "采购评估_" + ordernumer + ".xls";
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(newfilename, Standard, sheetName, pwd);
                filename = exAgent.FileName;
                FillInExcelData(3,1);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                exAgent.Save();
                exAgent.Close();
            }
            return filename;
        }
    }
}